package com.zerocarbon.framework.core.context;

import cn.hutool.core.convert.Convert;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.zerocarbon.framework.common.constants.ContextConstants;
import com.zerocarbon.framework.common.utils.StrPool;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取当前域中的 用户id appid 用户昵称
 * 注意： appid 通过token解析，  用户id 和 用户昵称必须在前端 通过请求头的方法传入。 否则这里无法获取
 * @author wyf  2020年6月2日
 */
public class UserContextHandler {
	
    private static final ThreadLocal<Map<String, String>> CONTEXT = new TransmittableThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, String> map = getLocalMap();
        map.put(key, value == null ? StrPool.EMPTY : value.toString());
    }

    public static <T> T get(String key, Class<T> type) {
        Map<String, String> map = getLocalMap();
        return Convert.convert(type, map.get(key));
    }

    public static <T> T get(String key, Class<T> type, Object def) {
        Map<String, String> map = getLocalMap();
        return Convert.convert(type, map.getOrDefault(key, String.valueOf(def == null ? "" : def)));
    }

    public static String get(String key) {
        Map<String, String> map = getLocalMap();
        return map.getOrDefault(key, "");
    }

    public static Map<String, String> getLocalMap() {
        Map<String, String> map = CONTEXT.get();
        if (map == null) {
            map = new HashMap<>(10);
            CONTEXT.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, String> threadLocalMap) {
    	CONTEXT.set(threadLocalMap);
    }

    public static Long getTenantId() {
        return get(ContextConstants.JWT_KEY_TENANT_ID, Long.class, 0L);
    }

    public static void setTenantId(Long tenantId) {
        set(ContextConstants.JWT_KEY_TENANT_ID, tenantId);
    }

    public static Long getAccountId() {
        return get(ContextConstants.JWT_KEY_ACCOUNT_ID, Long.class, 0L);
    }

    public static void setAccountId(Long accountId) {
        set(ContextConstants.JWT_KEY_ACCOUNT_ID, accountId);
    }

    public static Long getUserId() {
        return get(ContextConstants.JWT_KEY_USER_ID, Long.class, 0L);
    }

    public static void setUserId(Long userId) {
        set(ContextConstants.JWT_KEY_USER_ID, userId);
    }

    public static Integer getUserIdentity() {
        return get(ContextConstants.JWT_KEY_USER_IDENTITY, Integer.class);
    }

    public static void setUserIdentity(Integer userIdentity) {
        set(ContextConstants.JWT_KEY_USER_IDENTITY, userIdentity);
    }

    public static String getUserMobile() {
        return get(ContextConstants.JWT_KEY_USER_MOBILE, String.class);
    }

    public static void setUserMobile(String userMobile) {
        set(ContextConstants.JWT_KEY_USER_MOBILE, userMobile);
    }

    public static String getUserName() {
        return get(ContextConstants.JWT_KEY_USER_NAME, String.class);
    }

    public static void setUserName(String userName) {
        set(ContextConstants.JWT_KEY_USER_NAME, userName);
    }

    public static Long getCompanyId() {
        return get(ContextConstants.JWT_KEY_COMPANY_ID, Long.class, 0L);
    }

    public static void setCompanyId(Long orgId) {
        set(ContextConstants.JWT_KEY_COMPANY_ID, orgId);
    }

    public static String getCompanyName() {
        return get(ContextConstants.JWT_KEY_COMPANY_NAME, String.class);
    }

    public static void setCompanyName(String companyName) {
        set(ContextConstants.JWT_KEY_COMPANY_NAME, companyName);
    }

    public static void remove() {
        if (CONTEXT != null) {
        	CONTEXT.remove();
        }
    }
}
